home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 October: Mac OS SDK / Dev.CD Oct 97 SDK1.toast / Development Kits (Disc 1) / Interfaces&Libraries / Universal / Interfaces / CIncludes / Math64.h < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-12  |  11.7 KB  |  443 lines  |  [TEXT/MPS ]

  1. /*
  2.      File:        Math64.h
  3.  
  4.      Contains:    64-bit integer math Interfaces.
  5.  
  6.      Version:    Technology:    System 7.5
  7.                  Release:    Universal Interfaces 3.0.1
  8.  
  9.      Copyright:    © 1994-1997 by Apple Computer, Inc., all rights reserved
  10.  
  11.      Bugs?:        Please include the the file and version information (from above) with
  12.                  the problem description.  Developers belonging to one of the Apple
  13.                  developer programs can submit bug reports to:
  14.  
  15.                      devsupport@apple.com
  16.  
  17. */
  18. #ifndef __MATH64__
  19. #define __MATH64__
  20.  
  21. #ifndef __TYPES__
  22. #include <Types.h>
  23. #endif
  24.  
  25. #include <limits.h>
  26.  
  27.  
  28. #if PRAGMA_ONCE
  29. #pragma once
  30. #endif
  31.  
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35.  
  36. #if PRAGMA_IMPORT
  37. #pragma import on
  38. #endif
  39.  
  40. #if PRAGMA_STRUCT_ALIGN
  41.     #pragma options align=mac68k
  42. #elif PRAGMA_STRUCT_PACKPUSH
  43.     #pragma pack(push, 2)
  44. #elif PRAGMA_STRUCT_PACK
  45.     #pragma pack(2)
  46. #endif
  47.  
  48.  
  49. /*--------------------------------------------------------------------------------
  50.                 These routines are intended to provide C software support for
  51.                 64 bit integer types.  Their behavior should mimic anticipated
  52.                 64 bit hardware. This implementation should replace use of the
  53.                 "wide" type found in PowerPC.
  54.  
  55.     The following routines are available for performing math on 64-bit integers:
  56.     
  57.     S64Max
  58.                 Returns the largest representable SInt64.
  59.     S64Min
  60.                 Returns the smallest (i.e. most negative) SInt64.  Note: the negative
  61.                 (absolute value) of this number is not representable in an SInt64.
  62.                 That means that S64Negate(S64Min) is not representable (in fact,
  63.                 it returns S64Min).
  64.     S64Add
  65.                 Adds two integers, producing an integer result.  If an overflow
  66.                 occurs the result is congruent mod (2^64) as if the operands and
  67.                 result were unsigned.  No overflow is signaled.
  68.     
  69.     S64Subtract
  70.                 Subtracts two integers, producing an integer result.  If an overflow
  71.                 occurs the result is congruent mod (2^64) as if the operands and
  72.                 result were unsigned.  No overflow is signaled.
  73.  
  74.     S64Negate
  75.                 Returns the additive inverse of a signed number (i.e. it returns
  76.                 0 - the number).  S64Negate (S64Min) is not representable (in fact,
  77.                 it returns S64Min).
  78.     
  79.     S64Absolute
  80.                 Returns the absolute value of the number (i.e. the number if
  81.                 it is positive, or 0 - the number if it is negative).
  82.                 See S64Negate above.
  83.                 
  84.     S64Multiply
  85.                 Multiplies two signed numbers, producing a signed result.  Overflow
  86.                 is ignored and the low-order part of the product is returned.  The
  87.                 sign of the result is not guaranteed to be correct if the magnitude
  88.                 of the product is not representable.
  89.     S64Divide
  90.                 Divides dividend by divisor, returning the quotient.  The remainder
  91.                 is returned in *remainder if remainder (the pointer) is non-NULL.
  92.                 The sign of the remainder is the same as the sign of the dividend
  93.                 (i.e. it takes the absolute values of the operands, does the division,
  94.                 then fixes the sign of the quotient and remainder).  If the divisor
  95.                 is zero, then S64Max() will be returned (or S64Min() if the dividend
  96.                 is negative), and the remainder will be the dividend; no error is
  97.                 reported.
  98.     
  99.     S64Set
  100.                 Given an SInt32, returns an SInt64 with the same value.  Use this
  101.                 routine instead of coding 64-bit constants (at least when the
  102.                 constant will fit in an SInt32).
  103.     
  104.     S64SetU
  105.                 Given a UInt32, returns a SInt64 with the same value.
  106.     
  107.     S64Compare
  108.                 Given two signed numbers, left and right, returns an
  109.                 SInt32 that compares with zero the same way left compares with
  110.                 right.  If you wanted to perform a comparison on 64-bit integers
  111.                 of the form:
  112.                         operand_1 <operation> operand_2
  113.                 then you could use an expression of the form:
  114.                         xxxS64Compare(operand_1,operand_2) <operation> 0
  115.                 to test for the same condition.
  116.                 
  117.                 CAUTION: DO NOT depend on the exact value returned by this routine.
  118.                 Only the sign (i.e. positive, zero, or negative) of the result is
  119.                 guaranteed.
  120.  
  121.     S64And, S64Or, S64Eor and S64Not
  122.     
  123.                 Return Boolean (1 or 0) depending on the outcome of the logical
  124.                 operation.
  125.  
  126.     S64BitwiseAnd, S64BitwiseOr, S64BitwiseEor and S64BitwiseNot
  127.     
  128.                 Return the Bitwise result.
  129.                 
  130.     S64ShiftRight and S64ShiftLeft
  131.     
  132.                 The lower 7 bits of the shift argument determines the amount of 
  133.                 shifting.  S64ShiftRight is an arithmetic shift while U64ShiftRight
  134.                 is a logical shift.
  135.  
  136.     SInt64ToLongDouble
  137.                 
  138.                 Converts SInt64 to long double.  Note all SInt64s fit exactly into 
  139.                 long doubles, thus, the binary -> decimal conversion routines
  140.                 in fp.h can be used to achieve SInt64 -> long double -> decimal
  141.                 conversions.
  142.                 
  143.     LongDoubleToSInt64
  144.     
  145.                 Converts a long double to a SInt64.  Any decimal string that fits
  146.                 into a SInt64 can be converted exactly into a long double, using the
  147.                 conversion routines found in fp.h.  Then this routine can be used
  148.                 to complete the conversion to SInt64.
  149.                 
  150.     SInt64ToWide
  151.     
  152.                 Converts a SInt64 to a wide struct.  If SInt64 is implemented
  153.                 as a typedef of wide, the marco does nothing. If SInt64 is 
  154.                 implememnted as a long long, it casts the long long into a 
  155.                 wide struct.
  156.     
  157.     WideToSInt64
  158.     
  159.                 Converts a wide struct into a SInt64.  If SInt64 is implemented
  160.                 as a typedef of wide, the marco does nothing. If SInt64 is 
  161.                 implememnted as a long long, it reads the struct into a long long.
  162.     
  163.     
  164.     The corresponding UInt64 routines are also included.
  165.     
  166. --------------------------------------------------------------------------------*/
  167.  
  168.  
  169. #if TYPE_LONGLONG 
  170.  
  171. #define S64Max() LLONG_MAX
  172. #define S64Min() LLONG_MIN
  173. #define S64Add(x, y) ((x) + (y))
  174. #define S64Subtract(x, y) ((x) - (y))
  175. #define S64Negate(x) (-(x))
  176. #define S64Absolute(x) absll((x))
  177. #define S64Multiply(x, y) ((x) * (y))
  178. #define S64Div(x, y) ((x) / (y))
  179. #define S64Mod(x, y) ((x) % (y))
  180. #define S64Set(x) ((SInt64) (x))
  181. #define S64SetU(x) ((SInt64) (x))
  182. #define S32Set(x) ((SInt32) (x))
  183. #define S64Compare(x, y) ((int)((x) - (y)))
  184. #define S64And(x, y) ((Boolean)((x) && (y)))
  185. #define S64Or(x, y) ((Boolean)((x) || (y)))
  186. #define S64Eor(x, y) ((Boolean)((x) ^ (y)))
  187. #define S64Not(x) ((Boolean)(!(x)))
  188. #define S64BitwiseAnd(x, y) ((x) & (y))
  189. #define S64BitwiseOr(x, y) ((x) | (y))
  190. #define S64BitwiseEor(x, y) (((x) & (~(y))) | ((~(x)) & (y)))
  191. #define S64BitwiseNot(x) (~(x))
  192. #define S64ShiftRight(x, y) ((x) >> (y))
  193. #define S64ShiftLeft(x, y) ((x) << (y))
  194. #define SInt64ToLongDouble(x) ((long double)(x))
  195. #define LongDoubleToSInt64(x) ((SInt64)(x))
  196.  
  197. #define U64Max() ULLONG_MAX
  198. #define U64Add(x, y) ((x) + (y))
  199. #define U64Subtract(x, y) ((x) - (y))
  200. #define U64Multiply(x, y) ((x) * (y))
  201. #define U64Div(x, y) ((x) / (y))
  202. #define U64Mod(x, y) ((x) % (y))
  203. #define U64Set(x) ((UInt64) (x))
  204. #define U64SetU(x) ((UInt64) (x))
  205. #define U32SetU(x) ((UInt32) (x))
  206. #define U64Compare(x, y) ((int)((x) - (y)))
  207. #define U64And(x, y) ((Boolean)((x) && (y)))
  208. #define U64Or(x, y) ((Boolean)((x) || (y)))
  209. #define U64Eor(x, y) ((Boolean)((x) ^ (y)))
  210. #define U64Not(x) ((Boolean)(!(x)))
  211. #define U64BitwiseAnd(x, y) ((x) & (y))
  212. #define U64BitwiseOr(x, y) ((x) | (y))
  213. #define U64BitwiseEor(x, y) (((x) & (~(y))) | ((~(x)) & (y)))
  214. #define U64BitwiseNot(x) (~(x))
  215. #define U64ShiftRight(x, y) ((x) >> (y))
  216. #define U64ShiftLeft(x, y) ((x) << (y))
  217. #define UInt64ToLongDouble(x) ((long double)(x))
  218. #define LongDoubleToUInt64(x) ((UInt64)(x))
  219. #define UInt64ToSInt64(x) ((SInt64)(x))
  220. #define SInt64ToUInt64(x) ((UInt64)(x))
  221.  
  222. #else  
  223.  
  224. EXTERN_API_C( SInt64 )
  225. S64Max                            (void);
  226.  
  227. EXTERN_API_C( SInt64 )
  228. S64Min                            (void);
  229.  
  230. EXTERN_API_C( SInt64 )
  231. S64Add                            (SInt64                 x,
  232.                                  SInt64                 y);
  233.  
  234. EXTERN_API_C( SInt64 )
  235. S64Subtract                        (SInt64                 left,
  236.                                  SInt64                 right);
  237.  
  238. EXTERN_API_C( SInt64 )
  239. S64Negate                        (SInt64                 value);
  240.  
  241. EXTERN_API_C( SInt64 )
  242. S64Absolute                        (SInt64                 value);
  243.  
  244. EXTERN_API_C( SInt64 )
  245. S64Multiply                        (SInt64                 xparam,
  246.                                  SInt64                 yparam);
  247.  
  248. EXTERN_API_C( SInt64 )
  249. S64Divide                        (SInt64                 dividend,
  250.                                  SInt64                 divisor,
  251.                                  SInt64 *                remainder);
  252.  
  253. EXTERN_API_C( SInt64 )
  254. S64Set                            (SInt32                 value);
  255.  
  256. EXTERN_API_C( SInt64 )
  257. S64SetU                            (UInt32                 value);
  258.  
  259. EXTERN_API_C( SInt32 )
  260. S32Set                            (SInt64                 value);
  261.  
  262. EXTERN_API_C( int )
  263. S64Compare                        (SInt64                 left,
  264.                                  SInt64                 right);
  265.  
  266. EXTERN_API_C( Boolean )
  267. S64And                            (SInt64                 left,
  268.                                  SInt64                 right);
  269.  
  270. EXTERN_API_C( Boolean )
  271. S64Or                            (SInt64                 left,
  272.                                  SInt64                 right);
  273.  
  274. EXTERN_API_C( Boolean )
  275. S64Eor                            (SInt64                 left,
  276.                                  SInt64                 right);
  277.  
  278. EXTERN_API_C( Boolean )
  279. S64Not                            (SInt64                 value);
  280.  
  281. EXTERN_API_C( SInt64 )
  282. S64BitwiseAnd                    (SInt64                 left,
  283.                                  SInt64                 right);
  284.  
  285. EXTERN_API_C( SInt64 )
  286. S64BitwiseOr                    (SInt64                 left,
  287.                                  SInt64                 right);
  288.  
  289. EXTERN_API_C( SInt64 )
  290. S64BitwiseEor                    (SInt64                 left,
  291.                                  SInt64                 right);
  292.  
  293. EXTERN_API_C( SInt64 )
  294. S64BitwiseNot                    (SInt64                 value);
  295.  
  296. EXTERN_API_C( SInt64 )
  297. S64ShiftRight                    (SInt64                 value,
  298.                                  UInt32                 shift);
  299.  
  300. EXTERN_API_C( SInt64 )
  301. S64ShiftLeft                    (SInt64                 value,
  302.                                  UInt32                 shift);
  303.  
  304. /*
  305.     "long double" means 128 bit type on PowerPC and 80-bit type on 68K
  306. */
  307. EXTERN_API_C( long double )
  308. SInt64ToLongDouble                (SInt64                 value);
  309.  
  310. EXTERN_API_C( SInt64 )
  311. LongDoubleToSInt64                (long double             value);
  312.  
  313. EXTERN_API_C( UInt64 )
  314. U64Max                            (void);
  315.  
  316. EXTERN_API_C( UInt64 )
  317. U64Add                            (UInt64                 x,
  318.                                  UInt64                 y);
  319.  
  320. EXTERN_API_C( UInt64 )
  321. U64Subtract                        (UInt64                 left,
  322.                                  UInt64                 right);
  323.  
  324. EXTERN_API_C( UInt64 )
  325. U64Multiply                        (UInt64                 xparam,
  326.                                  UInt64                 yparam);
  327.  
  328. EXTERN_API_C( UInt64 )
  329. U64Divide                        (UInt64                 dividend,
  330.                                  UInt64                 divisor,
  331.                                  UInt64 *                remainder);
  332.  
  333. EXTERN_API_C( UInt64 )
  334. U64Set                            (SInt32                 value);
  335.  
  336. EXTERN_API_C( UInt64 )
  337. U64SetU                            (UInt32                 value);
  338.  
  339. EXTERN_API_C( UInt32 )
  340. U32SetU                            (UInt64                 value);
  341.  
  342. EXTERN_API_C( int )
  343. U64Compare                        (UInt64                 left,
  344.                                  UInt64                 right);
  345.  
  346. EXTERN_API_C( Boolean )
  347. U64And                            (UInt64                 left,
  348.                                  UInt64                 right);
  349.  
  350. EXTERN_API_C( Boolean )
  351. U64Or                            (UInt64                 left,
  352.                                  UInt64                 right);
  353.  
  354. EXTERN_API_C( Boolean )
  355. U64Eor                            (UInt64                 left,
  356.                                  UInt64                 right);
  357.  
  358. EXTERN_API_C( Boolean )
  359. U64Not                            (UInt64                 value);
  360.  
  361. EXTERN_API_C( UInt64 )
  362. U64BitwiseAnd                    (UInt64                 left,
  363.                                  UInt64                 right);
  364.  
  365. EXTERN_API_C( UInt64 )
  366. U64BitwiseOr                    (UInt64                 left,
  367.                                  UInt64                 right);
  368.  
  369. EXTERN_API_C( UInt64 )
  370. U64BitwiseEor                    (UInt64                 left,
  371.                                  UInt64                 right);
  372.  
  373. EXTERN_API_C( UInt64 )
  374. U64BitwiseNot                    (UInt64                 value);
  375.  
  376. EXTERN_API_C( UInt64 )
  377. U64ShiftRight                    (UInt64                 value,
  378.                                  UInt32                 shift);
  379.  
  380. EXTERN_API_C( UInt64 )
  381. U64ShiftLeft                    (UInt64                 value,
  382.                                  UInt32                 shift);
  383.  
  384. /*
  385.     "long double" means 128 bit type on PowerPC and 80-bit type on 68K
  386. */
  387. EXTERN_API_C( long double )
  388. UInt64ToLongDouble                (UInt64                 value);
  389.  
  390. EXTERN_API_C( UInt64 )
  391. LongDoubleToUInt64                (long double             value);
  392.  
  393. EXTERN_API_C( SInt64 )
  394. UInt64ToSInt64                    (UInt64                 value);
  395.  
  396. EXTERN_API_C( UInt64 )
  397. SInt64ToUInt64                    (SInt64                 value);
  398.  
  399. #endif /* TYPE_LONGLONG */
  400.  
  401. /* 
  402.     Functions to convert between [Unsigned]Wide and [S|U]Int64 types.
  403.     
  404.     These functions are necessary if source code which uses both
  405.     wide and SInt64 is to compile under a compiler that supports
  406.     long long.
  407. */
  408. #if TYPE_LONGLONG 
  409.     #define SInt64ToWide(x)         (*((wide*)(&x)))
  410.     #define WideToSInt64(x)         (*((SInt64*)(&x)))
  411.     #define UInt64ToUnsignedWide(x) (*((UnsignedWide*)(&x)))
  412.     #define UnsignedWideToUInt64(x) (*((UInt64*)(&x)))
  413. #else
  414.     #define SInt64ToWide(x)         (x)
  415.     #define WideToSInt64(x)         (x)
  416.     #define UInt64ToUnsignedWide(x) (x)
  417.     #define UnsignedWideToUInt64(x) (x)
  418. #endif
  419.  
  420.  
  421.  
  422.  
  423. #if PRAGMA_STRUCT_ALIGN
  424.     #pragma options align=reset
  425. #elif PRAGMA_STRUCT_PACKPUSH
  426.     #pragma pack(pop)
  427. #elif PRAGMA_STRUCT_PACK
  428.     #pragma pack()
  429. #endif
  430.  
  431. #ifdef PRAGMA_IMPORT_OFF
  432. #pragma import off
  433. #elif PRAGMA_IMPORT
  434. #pragma import reset
  435. #endif
  436.  
  437. #ifdef __cplusplus
  438. }
  439. #endif
  440.  
  441. #endif /* __MATH64__ */
  442.  
  443.